package xyz.zenheart.ffmpegrelay.controller;

/**
 * <p>项目名称: rtsp-face-master </p>
 * <p>描述: TODO </p>
 * <p>创建时间: 2021/9/7 </p>
 * <p>公司信息: 维之星研发部</p>
 *
 * @author CKM
 * @version v1.0
 */

import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.opencv.presets.opencv_objdetect;

import javax.swing.*;


public class RecordCameraPullTest1 {

    public static void main(String[] args) throws  Exception {
        Loader.load(opencv_objdetect.class);
        FrameGrabber grabber = FFmpegFrameGrabber.createDefault("rtsp://admin:admin@192.168.1.59:554/");
        grabber.setOption("rtsp_transport", "tcp");
//        grabber.setImageWidth(860);
//        grabber.setImageHeight(480);
//        grabber.setFormat("rtsp");
//        grabber.setVideoCodec(8);
//        grabber.setFrameRate(25);
        try {
            grabber.start();
        } catch (Exception e) {
            try {
                grabber.restart();
            } catch (Exception e1) {
                throw e;
            }
        }

        CanvasFrame canvas = new CanvasFrame("shexiangtouyulan");
        canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        while (canvas.isDisplayable()){
            canvas.showImage(grabber.grab());
        }
        grabber.close();

//        OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
//        Frame grabframe = grabber.grab();
//        IplImage grabbedImage = null;
//        if (grabframe != null) {
//            converter.convert(grabframe);
//        } else {
//        }
//        FrameRecorder recorder = FrameRecorder.createDefault("C:\\Users\\reality\\Desktop\\src\\1.mp4", 640, 360);
//        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); // avcodec.AV_CODEC_ID_H264
//        recorder.setFormat("flv");
//        recorder.setFrameRate(25);
//        recorder.setGopSize(10);
//        try {
//            recorder.start();
//        } catch (FrameRecorder.Exception e) {
//            try {
//                recorder.stop(); // 停止录制器的执行状态
//                recorder.start(); // 开启录制器
//            } catch (FrameRecorder.Exception e1) {
//                throw e;
//            }
//        }
//        while ((grabframe = grabber.grab()) != null ) {
//            grabbedImage = converter.convert(grabframe);
//            Frame rotatedFrame = converter.convert(grabbedImage);
//            if (startTimestamp == 0) {
//                startTimestamp = System.currentTimeMillis();
//            }
//            recorder.setTimestamp(1000 * (System.currentTimeMillis() - startTimestamp));// 时间戳
//            if (rotatedFrame != null) {
//                recorder.record(rotatedFrame);
//            }
//        }
//        recorder.stop();
//        recorder.release();
        grabber.stop();
    }
}